/*
 * @FilePath: /main.cpp
 * @Author: Wilson
 * @Descripttion: 
 * @Date: 2021-08-19 19:42:02
 * @LastEditors: Wilson
 * @LastEditTime: 2021-12-16 10:10:33
 */
#include <signal.h>
#include <thread>

#include <glog/logging.h>
#include <gflags/gflags.h>
#include <boost/filesystem.hpp>

#include "app/app.h"

bool InitGlog(char *appName)
{
    FLAGS_log_dir = "./log/";
    // 设置 google::INFO 级别的日志存储路径和文件名前缀
    google::SetLogDestination(google::GLOG_INFO, "./log/info_");
    // 设置 google::FATAL 级别的日志存储路径和文件名前缀
    google::SetLogDestination(google::GLOG_FATAL, "./log/fatal_");
    //设置 google::ERROaR 级别的日志存储路径和文件名前缀
    google::SetLogDestination(google::GLOG_ERROR, "./log/error_");
    //设置 google::WARNING 级别的日志存储路径和文件名前缀
    google::SetLogDestination(google::GLOG_WARNING, "./log/warning_");

    google::InitGoogleLogging(appName);

    //设置级别高于 google::INFO 的日志同时输出到屏幕
    google::SetStderrLogging(google::GLOG_INFO);

    //设置输出到屏幕的日志显示相应颜色
    FLAGS_colorlogtostderr = true;

    boost::filesystem::remove_all("./log");
    boost::filesystem::create_directories("./log");

    //缓冲日志输出，默认为30秒，此处改为立即输出
    FLAGS_logbufsecs = 0;
    //最大日志大小为 2MB
    FLAGS_max_log_size = 2;
    //捕捉 core dumped (linux)
    google::InstallFailureSignalHandler();

    return true;
}

//信号处理
static void signal_handler(int sig)
{
    LOG(WARNING)<<"signal_handler exit";
}

bool init()
{
    app::init();
    return true;
}

int main(int argc, char **argv)
{
    //SIGINT: 键盘输入以终止进程（ctrl + C)
    signal(SIGINT, signal_handler);
    
    gflags::ParseCommandLineFlags(&argc, &argv, true);
    InitGlog(argv[0]);
    
    init();

    google::ShutdownGoogleLogging();
    google::ShutDownCommandLineFlags();

    return 0;
}